package org.black.phoneUtility.sms.reply.app.fragment;

import java.io.Serializable;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class SmsReplyReplyDateTimeDialogFragment extends DialogFragment {
	public static final String TAG = SmsReplyReplyDateTimeDialogFragment.class
			.getName();

	public static final String ARGEMENTS_REPLY_DATE_TIME_TYPE = "replyDateTimeType";

	public static class ReplyDateTimeType implements Serializable {
		private static final long serialVersionUID = -1696039319670981316L;
		public static ReplyDateTimeType ALWAYS = new ReplyDateTimeType(1024);
		public static ReplyDateTimeType EXPIRED_AFTER = new ReplyDateTimeType(
				128);
		public static ReplyDateTimeType PERIOD = new ReplyDateTimeType(129);
		public static ReplyDateTimeType REPEAT = new ReplyDateTimeType(130);
		private int code;

		private ReplyDateTimeType(int code) {
			this.code = code;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + code;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ReplyDateTimeType other = (ReplyDateTimeType) obj;
			if (code != other.code)
				return false;
			return true;
		}
	}

	public interface SmsReplyReplyDateTimeListener {
		public void onChooseReplyDurationType(
				ReplyDateTimeType replyDateTimeType);
	}

	private SmsReplyReplyDateTimeListener smsReplyReplyDateTimeListener;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(
				R.string.sms_reply_add_edit_response_duration_setting_title)
				.setItems(R.array.sms_reply_date_time_choices,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								switch (which) {
								case 0:
									smsReplyReplyDateTimeListener
											.onChooseReplyDurationType(ReplyDateTimeType.ALWAYS);
									break;
								case 1:
									smsReplyReplyDateTimeListener
											.onChooseReplyDurationType(ReplyDateTimeType.EXPIRED_AFTER);
									break;
								case 2:
									smsReplyReplyDateTimeListener
											.onChooseReplyDurationType(ReplyDateTimeType.PERIOD);
									break;
								case 3:
									smsReplyReplyDateTimeListener
											.onChooseReplyDurationType(ReplyDateTimeType.REPEAT);
									break;
								default:
									// TODO: log some thing wrong
									break;
								}
							}
						});
		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyDateTimeListener = (SmsReplyReplyDateTimeListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyDurationListener");
		}
	}
}
